﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Mvc;
using WStest.Models;


namespace WStest.Models
{
    public class ToWaMembershipProvider : MembershipProvider
    {

        ServiceReference1.ToWaWebServiceClient ws = new ServiceReference1.ToWaWebServiceClient();
        
        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            throw new NotImplementedException();
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            if (string.IsNullOrEmpty(username))
            {
                status = MembershipCreateStatus.InvalidUserName;
                return null;
            }
            if (string.IsNullOrEmpty(password))
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }
            if (string.IsNullOrEmpty(email))
            {
                status = MembershipCreateStatus.InvalidEmail;
                return null;
            }

            ServiceReference1.userView user = null;
            try
            {
                user = ws.getUserByUsername(username);
            }
            catch { }

            if (user != null)
            {
                status = MembershipCreateStatus.DuplicateUserName;
                return null;
            }
            
            if (ws.IsEmailExist(email))
            {
                status = MembershipCreateStatus.DuplicateEmail;
                return null;
            }

            ToWaMembershipUser ToWaUser = new ToWaMembershipUser("ToWaMembershipProvider",
                    username,
                    null,
                    password,
                    null,
                    null,
                    isApproved,
                    false,
                    System.DateTime.Now,
                    System.DateTime.Now,
                    System.DateTime.Now,
                    System.DateTime.Now,
                    System.DateTime.Now
                    );

            //User ownUser = new User { Username = username, Password = password, Email = email };

            //db.Users.Add(ownUser);
            //db.SaveChanges();
            ws.register(username, password, " ", " ", " ", " ", " ", " ", email);

            status = MembershipCreateStatus.Success;

            return ToWaUser;
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new NotImplementedException();
        }

        public override bool EnablePasswordReset
        {
            get { throw new NotImplementedException(); }
        }

        public override bool EnablePasswordRetrieval
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override string GetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            ServiceReference1.userView user = null;
            try
            {
                user = ws.getUserByUsername(username);
            }
            catch { }
            if (user != null)
            {
                ToWaMembershipUser memUser = new ToWaMembershipUser("ToWaMembershipProvider",
                    user.username,
                    null,
                    user.username,
                    user.email,
                    "" + user.zipcode,
                    true,
                    false,
                    System.DateTime.Now,
                    System.DateTime.Now,
                    System.DateTime.Now,
                    System.DateTime.Now,
                    System.DateTime.Now);

                //memUser.ToWaId = user.Id;
                //memUser.ToWaUserName = user.Username;
                //memUser.ToWaType = "" + user.Type;

                return memUser;
            }
            return null;
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override string GetUserNameByEmail(string email)
        {
            throw new NotImplementedException();
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredPasswordLength
        {
            get { return 1; }
        }

        public override int PasswordAttemptWindow
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { throw new NotImplementedException(); }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresUniqueEmail
        {
            get { throw new NotImplementedException(); }
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }

        public override bool ValidateUser(string username, string password)
        {

            ServiceReference1.userView user = null;
            try
            {
                user = ws.validate(username, password);
                return true;
            }
            catch (System.ArgumentNullException ArgumentException)
            {
                ArgumentException.ToString();
            }
            catch (System.InvalidOperationException InvalidOpException)
            {
                InvalidOpException.ToString();
            }

            return false;
        }

    }
}